﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;

namespace SCOSub.Effects
{
    interface IChooseEffectStylable
    {
        string Run(string source);
    }
    
    class conansPrivate : ChangeFormatEffects, IChooseEffectStylable //conans专用
    {
        public string Run(string source)
        {
            StringReader sr = new StringReader(source);
            StringBuilder sb = new StringBuilder();
            sb.Append(CheckHeadFile());

            while (sr.Peek() > -1)
            {
                string sou = sr.ReadLine();
                List<TimeAndText > list = AddSourceText(sou);
                for (int i = 0; i < list.Count - 1; i++)
                {
                    TimeAndText tat_start = list[i];
                    TimeAndText tat_end = list[i + 1];
                    tat_start = TimeChangeCalc(ref tat_start, -50);
                    tat_end = TimeChangeCalc(ref tat_end, 50);
                    string starttime = FormatTimeToNormal(ref tat_start);
                    string endtime = FormatTimeToNormal(ref tat_end);
                    sb.Append(string.Format("Dialogue: 0,{0},{1},optx2,,0000,0000,0000,,{{\\fad(80,80)}}{2}\n", starttime, endtime, tat_start.Text)); //MessageBox.Show(tat_start.Text);
                }
                sb.Append("\n");
            }
            sr.Dispose();
            return sb.ToString();
        }
    }
    class BasicKaraoKe : ChangeFormatEffects, IChooseEffectStylable//基本卡拉OK
    {
        public string Run(string source)
        {
            StringReader sr = new StringReader(source);
            string output = CheckHeadFile();
            while (sr.Peek() > -1)
            {
                string sou = sr.ReadLine();
                List<TimeAndText> list = AddSourceText(sou);
                if (list.Count < 2)
                    continue;
                int[] array_ms = getMilliSecondArray(list);
                string lineStartTime, lineEndTime;
                getLineStartEndFormatedTime(list, -100, +100, out lineStartTime, out lineEndTime);
                string single = string.Format("Dialogue: 0,{0},{1},optx2,,0000,0000,0000,KaraoKe,{{\\pos(30,30)}}", lineStartTime, lineEndTime);
                for (int i = 0; i < list.Count - 1; i++)
                {
                    int dt = (int)Math.Round((double)((array_ms[i + 1] - array_ms[i]) / 10), 0);
                    single += string.Format("{{\\K{0}}}{1}", dt, list[i].Text);
                }
                output += single + "\n";
            }
            sr.Dispose();
            return output;
        }
    }
    class FadeKaraoKe : ChangeFormatEffects, IChooseEffectStylable //带渐变的卡拉OK
    {
        public string Run(string source)
        {
            StringReader sr = new StringReader(source);
            string output = CheckHeadFile();
            while (sr.Peek() > -1)
            {
                string sou = sr.ReadLine();

                List<TimeAndText> list = AddSourceText(sou);
                if (list.Count < 2)
                    continue;
                int[] array_ms = getMilliSecondArray(list);
                string lineStartTime, lineEndTime;
                getLineStartEndFormatedTime(list, -300, +200, out lineStartTime, out lineEndTime);

                string single = string.Format("Dialogue: 0,{0},{1},optx2,,0000,0000,0000,KaraoKe,{{\\pos(30,30)}}{{\\K20}}{{\\fad(200,200)}}", lineStartTime, lineEndTime);
                for (int i = 0; i < list.Count - 1; i++)
                {
                    int dt = (int)Math.Round((double)((array_ms[i + 1] - array_ms[i]) / 10), 0);
                    single += string.Format("{{\\K{0}}}{1}", dt, list[i].Text);
                }
                output += single + "\n";
            }
            sr.Dispose();
            return output;
        }
    }
    class HalfTime : ChangeFormatEffects, IChooseEffectStylable//二分时间段
    {
        public string Run(string source)
        {
            StringReader sr = new StringReader(source);
            string output = CheckHeadFile();
            while (sr.Peek() > -1)
            {
                string sou = sr.ReadLine();
                List<TimeAndText> list = AddSourceText(sou);
                if (list.Count < 2)
                    continue;
                int[] ms_offset = getMilliSecondOffset(list);
                int[] array_ms = getMilliSecondArray(list);
                string lineStartTime, lineEndTime;
                getLineStartEndFormatedTime(list, -50, +50, out lineStartTime, out lineEndTime);

                string single = string.Format("Dialogue: 0,{0},{1},optx2,,0000,0000,0000,,{{\\pos(320,80)}}", lineStartTime, lineEndTime);
                for (int i = 0; i < list.Count - 1; i++)
                {
                    int start = ms_offset[i] + 50;
                    int end = ms_offset[i + 1] + 50;
                    int mid = (int)((start + end) / 2);
                    single += string.Format("{{\\r\\t({0},{1},动作1)\\t({2},{3},动作2)}}{4}", start, mid, mid, end, list[i].Text);
                }
                output += single + "\n";
            }
            sr.Dispose();
            return output;
        }
    }
    class conansPrivateWithoutOverlay : ChangeFormatEffects, IChooseEffectStylable//conans专用,无覆盖
    {
        public string Run(string source)
        {
            StringReader sr = new StringReader(source);
            StringBuilder sb = new StringBuilder();
            sb.Append(CheckHeadFile());

            while (sr.Peek() > -1)
            {
                string sou = sr.ReadLine();
                List<TimeAndText> list = AddSourceText(sou);
                for (int i = 0; i < list.Count - 1; i++)
                {
                    TimeAndText tat_start = (TimeAndText)list[i];
                    TimeAndText tat_end = (TimeAndText)list[i + 1];
                    // tat_start = TimeChangeCalc(ref tat_start, -50);
                    // tat_end = TimeChangeCalc(ref tat_end, 50);
                    string starttime = FormatTimeToNormal(ref tat_start);
                    string endtime = FormatTimeToNormal(ref tat_end);
                    sb.Append(string.Format("Dialogue: 0,{0},{1},optx2,,0000,0000,0000,,{{\\fad(80,80)}}{2}\n", starttime, endtime, tat_start.Text)); //MessageBox.Show(tat_start.Text);
                }
                sb.Append("\n");
            }
            sr.Dispose();
            return sb.ToString();
        }
    }
}
